package com.cloud.schedule.dao;

import cn.hutool.core.util.StrUtil;
import com.cloud.schedule.bean.SpringScheduledCron;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhaoyangfu - 2020/7/29 17:22
 */
@Service
public class CronRepository {

    private static final List<SpringScheduledCron> cron = new ArrayList<>();

    static {
        cron.add(new SpringScheduledCron(0, "com.cloud.schedule.config.Task0", "0/1 * * * * ?", "1", true));
        cron.add(new SpringScheduledCron(1, "com.cloud.schedule.config.Task1", "0/1 * * * * ?", "1", true));
        cron.add(new SpringScheduledCron(2, "com.cloud.schedule.config.Task2", "0/1 * * * * ?", "1", true));
    }

    public List<SpringScheduledCron> findAll() {
        return cron;
    }

    public SpringScheduledCron findByCronId(Integer cronId) {
        return cron.get(cronId);
    }

    public SpringScheduledCron findByCronKey(String name) {
        for (SpringScheduledCron c : cron) {
            if (StrUtil.equals(c.getCronKey(), name)) {
                return c;
            }
        }
        return null;
    }
}
